<?php

/**
 * Default-Fiel-Renderer setzen
 */
pfField::setFieldRenderer(pfRendererHTML::getInstance());

abstract class pfField {

	/**
	 * Enter description here...
	 *
	 */
	const TYPE_HIDDEN   = 'hidden';
	const TYPE_TEXT     = 'text';
	const TYPE_TEXTAREA = 'textarea';
	const TYPE_CHECKBOX = 'checkbox';
	const TYPE_RADIO    = 'radio';
	const TYPE_BUTTON   = 'button';
	const TYPE_SUBMIT   = 'submit';
	const TYPE_SELECT   = 'select';

	/**
	 * 
	 * 
	 * @var array
	 */
	static private $factoryClasses = array(
		self::TYPE_HIDDEN   => 'pfFieldInput',
		self::TYPE_TEXT     => 'pfFieldInput',
		self::TYPE_TEXTAREA => 'pfFieldTextarea',
		self::TYPE_CHECKBOX => 'pfFieldInput',
		self::TYPE_RADIO    => 'pfFieldInput',
		self::TYPE_BUTTON   => 'pfFieldInput',
		self::TYPE_SUBMIT   => 'pfFieldInput',
	);

	/**
	 * Standard Formular Renderer
	 *
	 * @var pfRendererField
	 */
	static private $fieldRenderer;

	/**
	 * Enter description here...
	 *
	 * @var string
	 */
	private $name;

	/**
	 * Enter description here...
	 *
	 * @var string
	 */
	private $id;
	
	/**
	 * 
	 * @var string
	 */
	private $type;

	/**
	 * Enter description here...
	 *
	 * @param string $name
	 * @param string $type
	 * @param string $id
	 * 
	 * @return pfFieldDefintion
	 */
	static public function factory($name, $type, $id = null) {
		if (!isset(self::$factoryClasses[$type])) {
			new InvalidArgumentException($type . ' is not a valid field-definition!');	
		}

		$class = self::$factoryClasses[$type];
		
		return new $class($name, $type, $id);
	}

	/**
	 * Enter description here...
	 *
	 * @param pfRendererField $field
	 */
	static public function setFieldRenderer(pfRendererField $field) {
		self::$fieldRenderer = $field;
	}

	/**
	 * 
	 * @return pfRendererField
	 */
	static public function getFieldRenderer() {
		return self::$fieldRenderer;
	}

	/**
	 * Enter description here...
	 *
	 * @param string $name
	 * @param string $type
	 */
	protected function __construct($name, $type, $id = null) {
		//gucken, ob array-struktur -> validieren -> pfad auslesen für isset()...
		$this->name = $name;		
		$this->type = $type;

		// ggf übergeben oder aus name erzeugen
		$this->id   = ($id != null ? $id : $name);
	}

	/**
	 * Enter description here...
	 *
	 * @return string
	 */
	public function getName() {
		return $this->name;
	}

	/**
	 * Enter description here...
	 *
	 * @return string
	 */
	public function getType() {
		return $this->type;
	}

	/**
	 * Enter description here...
	 *
	 * @return string
	 */
	public function getId() {
		return $this->id;
	}
	
	/**
	 * 
	 * @return string
	 */
	final public function __toString() {
		return self::$fieldRenderer->render($this);
	}
}